package com.example.utils;

import java.io.RandomAccessFile;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;



public class Utils {
	/** �ֻ�����cmwap */
	public static final String NET_CMWAP = "cmwap";
	/** �ֻ�����3gwap */
	public static final String NET_WAP_3G = "3gwap";
	/** �ֻ�����uniwap */
	public static final String NET_UNIWAP = "uniwap";

	public static final String CMWAP = "cmwap";
	public static final String WAP_3G = "3gwap";
	public static final String UNIWAP = "uniwap";

	public static String CTWAP = "ctwap";
	public static String CTNET = "ctnet";

	
	/** ������� */
	public static final String CODE_HTTP_FAIL = "-1";
	/** dxMemberȡ����id��Ĭ�Ϸ���ֵ */
	public static final int ID_DXMEMBER = -1;
	/** �������errCode */
	public static final String CODE_ERROR_RIGHT = "0";
	/** ����ɹ� */
	public static final String CODE_HTTP_SUCCEED = "200";
	public static final String CODE_HTTPS_RECONNECT = "002";
	/** �Ự���� */
	public static final String CODE_SESSION_EXPIRED = "2000";
	/** ������ֹͣ���� */
	public static final String CODE_STOP_SERVER = "5000";
	public static final String SERVER_NOT_RESPONDING = "10000";
	/** û���ҵ����� */
	public static final String CODE_PAGE_NOT_FOUND = "404";
	/** �����ͻ��� */
	public static final String CODE_HTTP_RESTART_CLIENT = "4000";

	public static final String NET_WORK_INVAILABLE = "netInvailable";
	
	/**
	 * д���ļ�
	 * 
	 * @param str
	 */
	public synchronized static void writeFile(String content) {
////		if (TextUtils.isEmpty(content) || !isSDcardExist()) {
////			return;
////		}
//		try {
//			if (!file.exists()) {
//				file.createNewFile();
//			}
//			RandomAccessFile raf = new RandomAccessFile(file, "rw");
//			long len = raf.length();
//			raf.seek(len);
//			raf.writeBytes(content);
//			raf.close();
//		} catch (Exception e) {
//			LogUtil.e(TAG, e.toString());
//		}
	}
	
	
	/**
	 * ��������Ƿ����
	 */
	public static boolean isAvailable(Context context) {
		ConnectivityManager cwjManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cwjManager.getActiveNetworkInfo();
		if (info != null && info.isAvailable()) {
			return true;
		} else {
			return false;
		}
	}

	
	public static String getNetMode(Context context) {
		String netMode = "";
		try {
			final ConnectivityManager connectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			final NetworkInfo mobNetInfoActivity = connectivityManager
					.getActiveNetworkInfo();
			if (mobNetInfoActivity == null || !mobNetInfoActivity.isAvailable()) {
				netMode = NET_WORK_INVAILABLE;
			} else {
				int netType = mobNetInfoActivity.getType();
				if (netType == ConnectivityManager.TYPE_WIFI) {
					netMode = mobNetInfoActivity.getTypeName();
				} else if (netType == ConnectivityManager.TYPE_MOBILE) {
					netMode = mobNetInfoActivity.getExtraInfo();

				} else {
					// Do nothing
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			netMode = "";
		} finally {
			if ("epc.tmobile.com".equals(netMode) || "".equals(netMode)) {
				netMode = "3G";
				return netMode;
			}
		}
		return netMode;
	}
}
